package com.lesson00;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class TestActionEvent {
    public static void main(String[] args) {
        Frame frame = new Frame();
        // 创建一个按钮
        Button button = new Button();
        MyActionListener myActionListener = new MyActionListener();
        // 将事件监听器添加到按钮上
        button.addActionListener(myActionListener);
        frame.add(button, BorderLayout.CENTER);
        frame.pack();
        WindowClose(frame);//关闭窗口
        frame.setVisible(true);
    }

    //关闭事件
    private static void WindowClose(Frame frame) {
        frame.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

}

class MyActionListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {
        // 处理按钮点击事件
        System.out.println("按钮被点击了");
    }
}